﻿using System;
using System.Windows.Forms;
using mki.Backup.GUI.Enumerations;
using mki.Backup.GUI.Properties;

namespace mki.Backup.GUI
{
	/// <summary>
	/// Main Form Class
	/// </summary>
	internal partial class MainForm : Form
	{
		#region Constructors (1)

		/// <summary>
		/// Initializes a new instance of the <see cref="MainForm"/> class.
		/// </summary>
		public MainForm()
		{
			this.InitializeComponent();

			// init
			this.init();
		}

		#endregion Constructors

		#region Properties (6)

		/// <summary>
		/// Gets or sets the button click.
		/// </summary>
		/// <value>
		/// The button click.
		/// </value>
		public Action<Object, ButtonAction> ButtonClick { get; set; }

		/// <summary>
		/// Sets a value indicating whether [error grid visible].
		/// </summary>
		/// <value><c>true</c> if [error grid visible]; otherwise, <c>false</c>.</value>
		public Boolean ErrorGridVisible
		{
			set { this.errorDataGridView.Visible = value; }
		}

		/// <summary>
		/// Gets the error items binding source.
		/// </summary>
		/// <value>The error items binding source.</value>
		public BindingSource ErrorItemsBindingSource
		{
			get { return this.errorBindingSource; }
		}

		/// <summary>
		/// Gets the job properties.
		/// </summary>
		public PropertyGrid JobDetailProperties
		{
			get { return this.jobDetailPropertyGrid; }
		}

		/// <summary>
		/// Gets the job tree view.
		/// </summary>
		public TreeView JobTreeView
		{
			get { return this.jobsTreeView; }
		}

		/// <summary>
		/// Sets the save hint visible.
		/// </summary>
		/// <value>
		/// The save hint visible.
		/// </value>
		public Boolean SaveHintVisible
		{
			set { this.saveNoteLabel.Visible = value; }
		}

		#endregion Properties

		#region Methods (2)

		// Public Methods (1) 

		/// <summary>
		/// Sets the state of the service.
		/// </summary>
		/// <param name="buttonEnabled">if set to <c>true</c> [button enabled].</param>
		/// <param name="serviceStatus">The service status.</param>
		/// <param name="buttonText">The button text.</param>
		public void SetServiceState(Boolean buttonEnabled, String serviceStatus, String buttonText)
		{
			this.toggleServiceButton.Enabled = buttonEnabled;
			this.serviceStateLabel.Text = serviceStatus;
			this.toggleServiceButton.Text = buttonText;
		}
		// Private Methods (1) 

		/// <summary>
		/// Inits this instance.
		/// </summary>
		private void init()
		{
			this.jobsTreeView.HideSelection = false;
			this.errorDataGridView.RowStateChanged += (s, e) => this.ButtonClick(e.StateChanged == DataGridViewElementStates.Selected ? e.Row.DataBoundItem : null, ButtonAction.RowSelected);
			this.saveButton.Click += (s, e) => this.ButtonClick(s, (ButtonAction)((Button)s).Tag);
			this.saveButton.Tag = ButtonAction.Save;
			this.saveToolStripMenuItem.Tag = ButtonAction.Save;
			this.saveToolStripMenuItem.Click += (s, e) => this.ButtonClick(s, (ButtonAction)((ToolStripMenuItem)s).Tag);
			this.saveToolStripMenuItem.Image = Resources.disk;
			this.logfilesToolStripMenuItem.Image = Resources.page_white_stack;
			this.openGUILogfileToolStripMenuItem.Tag = ButtonAction.OpenGUILogFile;
			this.openGUILogfileToolStripMenuItem.Click += (s, e) => this.ButtonClick(s, (ButtonAction)((ToolStripMenuItem)s).Tag);
			this.openGUILogfileToolStripMenuItem.Image = Resources.page_white_wrench;
			this.openServiceLogfileToolStripMenuItem.Tag = ButtonAction.OpenServiceLogFile;
			this.openServiceLogfileToolStripMenuItem.Click += (s, e) => this.ButtonClick(s, (ButtonAction)((ToolStripMenuItem)s).Tag);
			this.openServiceLogfileToolStripMenuItem.Image = Resources.page_white_gear;
			this.clearLogfilesToolStripMenuItem.Tag = ButtonAction.ClearLogFiles;
			this.clearLogfilesToolStripMenuItem.Click += (s, e) => this.ButtonClick(s, (ButtonAction)((ToolStripMenuItem)s).Tag);
			this.clearLogfilesToolStripMenuItem.Image = Resources.page_white_delete;
			this.mailSettingsToolStripMenuItem.Tag = ButtonAction.SMTPSettings;
			this.mailSettingsToolStripMenuItem.Click += (s, e) => this.ButtonClick(s, (ButtonAction)((ToolStripMenuItem)s).Tag);
			this.mailSettingsToolStripMenuItem.Image = Resources.email_edit;
			this.exitToolStripMenuItem.Tag = ButtonAction.Exit;
			this.exitToolStripMenuItem.Click += (s, e) => this.ButtonClick(s, (ButtonAction)((ToolStripMenuItem)s).Tag);
			this.exitToolStripMenuItem.Image = Resources.door_in;
			this.helpOnlineToolStripMenuItem.Tag = ButtonAction.Help;
			this.helpOnlineToolStripMenuItem.Click += (s, e) => this.ButtonClick(s, (ButtonAction)((ToolStripMenuItem)s).Tag);
			this.helpOnlineToolStripMenuItem.Image = Resources.help;
			this.aboutToolStripMenuItem.Tag = ButtonAction.About;
			this.aboutToolStripMenuItem.Click += (s, e) => this.ButtonClick(s, (ButtonAction)((ToolStripMenuItem)s).Tag);
			this.aboutToolStripMenuItem.Image = Resources.information;
			this.toggleServiceButton.Click += (s, e) => this.ButtonClick(s, (ButtonAction)((Button)s).Tag);
			this.toggleServiceButton.Tag = ButtonAction.ToggleService;
		}

		#endregion Methods
	}
}